package com.web.test.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtil {

	private static String driver;
	private static String url;
	private static String username;
	private static String password;

	private static void getValue() throws IOException {

		String resource = "jdbc.properties";
		InputStream inStream = JDBCUtil.class.getClassLoader()
				.getResourceAsStream(resource);
		Properties properties = new Properties();
		properties.load(inStream);

		driver = properties.getProperty("driver");
		url = properties.getProperty("url");
		username = properties.getProperty("username");
		password = properties.getProperty("password");

	}

	public static Connection getConnection() throws ClassNotFoundException,
			SQLException, IOException {

		// 获取参数值
		getValue();
		// 步骤：1、打开驱动连接
		Class.forName(driver);
		// 2、驱动管理器;3、打开到具体数据库
		return DriverManager.getConnection(url, username, password);
	}

	public static void close(Connection conn, Statement st, ResultSet rs) throws SQLException {


		if (rs != null) {
			rs.close();
		}
		if (st != null) {
			st.close();
		}
		if (conn != null){
			conn.commit();
			conn.close();
		}
	}

}
